<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
//for stock
define('COLOR_NAME', 'Farve');
define('SIZE_NAME', 'Størrelse');

echo '<pre>';

function group_assoc($array, $key) {
    $return = array();
    foreach($array as $v) {
        $return[$v[$key]][] = $v;
    }
    return $return;
}

function toArray($obj)
{
    if (is_object($obj)) $obj = (array)$obj;
    if (is_array($obj)) {
        $new = array();
        foreach ($obj as $key => $val) {
            $new[$key] = toArray($val);
        }
    } else {
        $new = $obj;
    }
    return $new;
}

function getAttributeId($db, $KatalogNumber, $name){
	$attribute = $db->fetch_row('katalognumber = ' . $KatalogNumber .", name = " . $name, 'attribute' , FETCH_ASSOC);
	$exists = $db->get_rows();
	if(!empty($exists)){
		return $attribute[0]['attribute_id'];
	}
	return '';
}

function getAttributeValueId($db, $attribute_id, $name){
	$attribute_value = $db->fetch_row('attribute_id = ' . $attribute_id .", name = " . $name, 'attribute_value', FETCH_ASSOC);
	$exists = $db->get_rows();
	if(!empty($exists)){
		return $attribute_value[0]['value_id'];
	}
	return '';

}
require_once('simplesql.class.php5');

$ini = parse_ini_file("web.ini");
while(list($key,$value) = each($ini)){
	if($key == 'hostName'){
		$hostName = $value;
	}
	else if($key == 'userName'){
		$userName = $value;
	}
	else if($key == 'password'){
		$password = $value;
	}
	else if($key == 'dbName'){
		$dbName = $value;
	}
}

$db = new simplesql($hostName,$userName,$password,$dbName);
$db->query("SET NAMES utf8");

	$myarticles = file_get_contents('cache.txt');
	$objInfo = unserialize($myarticles);
	$arrInfo = toArray($objInfo);	
		
	$error = false;
	$db->query("BEGIN");
	
	$group_products = group_assoc($arrInfo, 'KatalogNumber');
	foreach($group_products as $KatalogNumber=>$products){
		/*
		//group by color
		$group_product_color = group_assoc($products, 'Vparam1');
		//get array with color name
		$arr_colors = array_keys($group_product_color);
		$color_name = COLOR_NAME;
		$color_description = $products[0]['SupplierName'] . " " . $products[0]['ItemGroupName'] . $products[0]['Kparam2'] . " " . $products[0]['Kparam1'];
		
		$attribute_id = getAttributeId($db, $KatalogNumber, COLOR_NAME);
		//$attribute_result = $db->fetch_row('katalognumber = ' . $KatalogNumber . ", kparam1 = " . $products[0]['Kparam1'], 'attribute', FETCH_ASSOC);
		
		
		//$exists = $db->get_rows();
		//delete all color and size on table attribute and attribute_value
		if(empty($attribute_id)){
			//add color 1 time cauze 1 katalogNumber has only 1 color
			$fields = array('name', 'description', 'date_added', 'status', 'kparam1', 'katalognumber');
			$data = array($color_name, $color_description, date('Y-m-d'), "1", $products[0]['Kparam1'], $KatalogNumber);
			$attribute_id = $db->insert($fields, $data, 'attribute');
		}		
		
		if(empty($attribute_id)){
			$error = true;
			break;
		}
		foreach($arr_colors as $color){
			//add color name to table attribute_value
			$exists = getAttributeValueId($db, $attribute_id, $color);
			if(empty($exists)){
				$fields = array('name', 'attribute_id');
				$data = array($color, $attribute_id);
				$value_id = $db->insert($fields, $data, 'attribute_value');
				if(!$value_id){
					$error = true;
					break;
				}
			}			
		}
		
		if(!$value_id){
			$error = true;
			break;
		}
		*/
		//group by size
		$group_product_size = group_assoc($products, 'Vparam4');
		//get array with size name
		$arr_sizes = array_keys($group_product_size);
		$size_name = "Størrelse";
		$size_description = $products[0]['SupplierName'] . "|" . $products[0]['ItemGroupName'] . "|" . $products[0]['Kparam2'] . "|" . $products[0]['Kparam1'];
		
		$attribute_id = getAttributeId($db, $KatalogNumber, SIZE_NAME);		
		if(empty($attribute_id)){
			//add size 1 time cauze 1 katalogNumber has only 1 size
			$fields = array('name', 'description', 'date_added', 'status', 'kparam1', 'katalognumber');
			$data = array($size_name, $size_description, date('Y-m-d'), "1", $products[0]['Kparam1'], $KatalogNumber);
			$attribute_id = $db->insert($fields, $data, 'attribute');
		}
		if(!$attribute_id){
			$error = true;
			break;
		}
		foreach($arr_sizes as $size){
			$exists = getAttributeValueId($db, $attribute_id, $size);
			if(empty($exists)){
				//add size name to table attribute_value				
				$fields = array('name', 'attribute_id');
				$data = array($size, $attribute_id);
				$value_id = $db->insert($fields, $data, 'attribute_value');				
				if(!$value_id){
					$error = true;
					break;
				}
			}
		}
		if(!$value_id){
			$error = true;
			break;
		}
		
	}
	if(!$error)  
	{  
	//*** Commit Transaction ***//  
		$db->query("COMMIT");  
		echo "OK <br />";  
		
	}  
	else  
	{  
		//*** RollBack Transaction ***//  
		echo "Error:  [".$db->get_lastquery()."]" ."-" . mysql_error();  
		$db->query("ROLLBACK");  
	}
	
	echo '</pre>';


?>
</body>
</html>